# encoding=utf-8
import redis
from constant.const import const
import datetime
from utils.string import dir2obj
from entity.entity import category

pool = redis.ConnectionPool(host='localhost', port='6379', db=15, password='lyf000000')
r = redis.Redis(connection_pool=pool)

# r.set('food', 'mutton', ex=3)
#
# r.setbit("users-subscribed", 7, 1)
# r.setbit("users-subscribed", 6, 1)
# print(r.getbit("users-subscribed", 7) == 0)
#
# # BITOP：对多个位数组进行按位与、或、异或运算。
# r.bitop("AND", "newkey", "users-subscribed", "users-subscribed-2")
# print(r.get("newkey"))

# result = r.bitfield("bit-map", "incrby", "i5", 100, 4, "get", u4, 2)
# print(result)

# BITCOUNT：统计位数组中值为1的二进制位数量。
# print(r.bitcount("users-subscribed"))
# print(r.bitcount("users-subscribed", 0, 4))
#
# r.hset('ex1', "k1", "v1")
# r.hset('ex2', "k2", "v2")
# print(r.expire('ex1', 10))
# extime = datetime.datetime(2023, 3, 9, 15, 17, 00)
# extime.strftime('%Y-%m-%d %H:%M:%S %f')
# print(r.expireat('ex2', extime))

# hget = r.hget("category_entity", "home")
# s = str(hget, encoding="utf-8")
# obj = dir2obj(eval(s), category)
# print(obj)

# pipe = r.pipeline()
# pipe_size = 100000
# len = 0
# key_list = []
# print(r.pipeline())
# keys = r.keys()
# print(keys)
# for key in keys:
#     key_list.append(key)
#     pipe.get(key)
#     if len < pipe_size:
#         len += 1
#     else:
#         for (k, v) in zip(key_list, pipe.execute()):
#             print(k, v)
#         len = 0
#         key_list = []
#
#


pipe = r.hgetall("category").items()
print(len(pipe))
for m in pipe:
    print(str(m[0], encoding="utf-8") + " " + str(m[1], encoding="utf-8"))
    # print(str(m[0], encoding="utf-8") + " " + m[1])

